import 'package:flutter/material.dart';
import 'package:flutter_demo/conf/m3.dart';
import 'package:flutter_demo/pages/page_with_title.dart';
import 'package:url_launcher/url_launcher.dart';

///app通用组件，这里主要是定义一些页面常用组件，避免在每个页面设置相同的参数不好统一调整，也简化
///了页面构建

///页面边距，这里设置为20
class PagePadding extends Padding {
  const PagePadding(Widget child)
      : super(
            key: null,
            padding:
                const EdgeInsetsDirectional.symmetric(horizontal: M3.paddingL),
            child: child);
}

///带主题色的标题
class PrimaryTitle extends StatelessWidget {
  final String text;

  const PrimaryTitle(this.text, {super.key});

  @override
  Widget build(BuildContext context) {
    return Text(
      text,
      style: Theme.of(context)
          .textTheme
          .titleMedium
          ?.copyWith(color: Theme.of(context).colorScheme.primary),
    );
  }
}

///分类标题
class CategoryTitle extends StatelessWidget {
  final String text;

  const CategoryTitle(this.text, {super.key});

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        const SizedBox(
          width: 5,
          child: Divider(height: 1),
        ),
        AppSpacer.normal,
        Text(
          text,
          style: Theme.of(context).textTheme.titleSmall,
        ),
        AppSpacer.normal,
        const Expanded(
          child: Divider(height: 1),
        ),
      ],
    );
  }
}

///正文文本
class BodyText extends StatelessWidget {
  final String text;

  const BodyText(this.text, {super.key});

  @override
  Widget build(BuildContext context) {
    return Text(
      text,
      style: Theme.of(context).textTheme.bodyMedium,
    );
  }
}

///描述文本
class LabelText extends StatelessWidget {
  final String text;

  const LabelText(this.text, {super.key});

  @override
  Widget build(BuildContext context) {
    return Text(
      text,
      style: Theme.of(context).textTheme.labelMedium,
    );
  }
}

///描述文本
class PrimaryHeaderLabelText extends StatelessWidget {
  final String header;
  final String? line1;
  final String? line2;
  final String? line3;
  final String? line4;
  final String? line5;
  final String? line6;
  final String? line7;
  final String? line8;
  final String? line9;

  const PrimaryHeaderLabelText(
    this.header, [
    this.line1,
    this.line2,
    this.line3,
    this.line4,
    this.line5,
    this.line6,
    this.line7,
    this.line8,
    this.line9,
  ]) : super(key: null);

  @override
  Widget build(BuildContext context) {
    return Text.rich(
      TextSpan(
        text: "- ",
        children: [
          TextSpan(
            text: header,
          ),
          TextSpan(
            text: " ${[
              if (line1 != null) line1,
              if (line2 != null) line2,
              if (line3 != null) line3,
              if (line4 != null) line4,
              if (line5 != null) line5,
              if (line6 != null) line6,
              if (line7 != null) line7,
              if (line8 != null) line8,
              if (line9 != null) line9,
            ].join("\n")}",
            style: Theme.of(context).textTheme.labelMedium,
          ),
        ],
      ),
      style: Theme.of(context)
          .textTheme
          .labelMedium
          ?.copyWith(color: Theme.of(context).colorScheme.primary),
    );
  }
}

///重要提示文本
class NoticeText extends StatelessWidget {
  final String text;

  const NoticeText(this.text, {super.key});

  @override
  Widget build(BuildContext context) {
    return Text(
      text,
      style: Theme.of(context)
          .textTheme
          .labelMedium
          ?.copyWith(color: Theme.of(context).colorScheme.error),
    );
  }
}

///页面内容间隔(注意，flutter本身有个Spacer组件用于占用所有剩余空间，不要弄混了)
class AppSpacer extends SizedBox {
  ///小间隔，用于图片与文本之间
  static const mini = AppSpacer._(M3.paddingXS, M3.paddingXS);

  ///常用间隔
  static const normal = AppSpacer._(M3.paddingS, M3.paddingS);

  ///大间隔
  static const large = AppSpacer._(M3.paddingM, M3.paddingM);

  const AppSpacer._(double width, double height)
      : super(width: width, height: height, key: null);
}

///无序列表项
class UlItem extends StatelessWidget {
  final String text;

  const UlItem(this.text, {super.key});

  @override
  Widget build(BuildContext context) {
    var colorScheme = Theme.of(context).colorScheme;
    return Container(
      padding: const EdgeInsetsDirectional.symmetric(
        horizontal: M3.paddingL,
        vertical: M3.paddingM,
      ),
      decoration: BoxDecoration(
        color: colorScheme.surfaceContainer,
        borderRadius: M3.connerRadiusMedium,
      ),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
            width: 12,
            height: 12,
            decoration: BoxDecoration(
              color: colorScheme.onSurface,
              borderRadius: BorderRadius.circular(6),
            ),
          ),
          AppSpacer.normal,
          Expanded(
            child: Text(
              text,
              style: Theme.of(context).textTheme.labelMedium?.copyWith(
                    color: colorScheme.onSurface,
                  ),
            ),
          ),
        ],
      ),
    );
  }
}

///有序列表项
class OlItem extends StatelessWidget {
  final int index;
  final String text;

  const OlItem(this.index, this.text, {super.key});

  @override
  Widget build(BuildContext context) {
    var colorScheme = Theme.of(context).colorScheme;
    return Container(
      padding: const EdgeInsetsDirectional.symmetric(
        horizontal: M3.paddingL,
        vertical: M3.paddingM,
      ),
      decoration: BoxDecoration(
        color: colorScheme.surfaceContainer,
        borderRadius: M3.connerRadiusMedium,
      ),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Text(
            "$index.",
            style: Theme.of(context).textTheme.labelMedium?.copyWith(
                  color: colorScheme.onSurface,
                ),
          ),
          AppSpacer.normal,
          Expanded(
            child: Text(
              text,
              style: Theme.of(context).textTheme.labelMedium?.copyWith(
                    color: colorScheme.onSurface,
                  ),
            ),
          ),
        ],
      ),
    );
  }
}

///跳转页面链接
class PageLink extends StatelessWidget {
  final String text;
  final PageWithTitle page;

  ///- [text] 链接显示的文本
  ///- [page] 目标页面组件
  const PageLink(this.page, {this.text = "", super.key});

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        page.push(context);
      },
      child: Text(
        "${page.title} $text",
        style: TextStyle(
            color: Theme.of(context).colorScheme.primary,
            decoration: TextDecoration.underline,
            decorationStyle: TextDecorationStyle.solid),
      ),
    );
  }
}

///网页页面链接
class UrlLink extends StatelessWidget {
  final String text;
  final String url;

  ///- [text] 链接显示的文本
  ///- [url] 链接地址
  const UrlLink(this.text, this.url, {super.key});

  // 打开浏览器的方法
  Future<void> _launchUrl() async {
    // 检查是否可以打开链接
    if (!await launchUrl(Uri.parse(url),
        mode: LaunchMode.externalApplication)) {
      // 无法打开时抛出异常（或处理错误）
      throw Exception('无法打开 $url');
    }
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: _launchUrl,
      child: Text(
        text,
        style: TextStyle(
            color: Theme.of(context).colorScheme.primary,
            decoration: TextDecoration.underline,
            decorationStyle: TextDecorationStyle.solid),
      ),
    );
  }
}
